<?php
require_once 'mods/class_generic_object.php';
class CompanyDepartment extends GenericObject
{
    public $companies;
    public function __construct()
    {
        parent::__construct();
        $this->_tableName='company_departments';
        $this->_properties['id']=null;
        $this->_properties['name']=null;
        $this->_properties['company']=null;
        $this->companies=new Collection();
        $this->companies->setLoadCallback('loadCompany', $this);
    }
    
    public function loadCompany(Collection $col)
    {
        $company=new Company();
        $company->load($this->company);
        $col->addItem($company);
    }
    
    public function set($id,$name,$company)
    {
        $this->id=$id;
        $this->name=$name;
        $this->company=$company;
    }
    
    public function getCompanyName()
    {
        return $this->companies->getItem()->name;
    }
    
    public function load($id = null)
    {
        if (!empty($id)) {
            $id=intval($id);
            $sql='SELECT id,name,company 
            FROM company_departments 
            WHERE id='.$id;
            $result=$this->_db->select($sql,false);
            $sizeofresult=sizeof($result,false);
            if ($sizeofresult) {
                list($id,$name,$company)=$result[0];
                $this->set($id,$name,$company);
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }
}
?>